<template>
  <div class="login-container">
    <!-- 登录 -->
    <div class="login-wrap">
      <div class="login">
        <div class="loginform">
          <!--          <ul class="tab clearFix">-->
          <!--            <li>-->
          <!--              <a href="#" style="border-right: 0">扫描登录</a>-->
          <!--            </li>-->
          <!--            <li>-->
          <!--              <a href="#" class="current">账户登录</a>-->
          <!--            </li>-->
          <!--          </ul>-->

          <el-tabs class="tab clearFix" v-model="activeName" type="card">
            <el-tab-pane label="账号登录" name="first">
              <div class="content">
                <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="80px">
                  <el-form-item class="input-text clearFix" label="用户名" prop="username" style="width: 320px;">
                    <el-input v-model="loginForm.username"></el-input>
                  </el-form-item>
                  <el-form-item class="input-text clearFix" label="密码" prop="password"
                                style="width: 320px; padding-right: 5px">
                    <el-input v-model="loginForm.password" type="password"></el-input>
                  </el-form-item>
                  <el-form-item class="input-text clearFix" label="验证码" prop="captcha" style="width: 320px;">
                    <el-input v-model="loginForm.captcha" style="width: 132px; float: left" maxlength="5"></el-input>
                    <el-image :src="captImg" class="captImg" @click="getCaptcha"></el-image>
                  </el-form-item>

                  <el-form-item class="input-text clearFix">
                    <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
                    <el-button @click="resetForm('loginForm')">重置</el-button>
                  </el-form-item>
                </el-form>

                <div class="call clearFix">
                  <ul>
                    <li><img src="./images/qq.png" alt=""/></li>
                    <li><img src="./images/sina.png" alt=""/></li>
                    <li><img src="./images/ali.png" alt=""/></li>
                    <li><img src="./images/weixin.png" alt=""/></li>
                  </ul>
                  <router-link class="register" to="/register">立即注册</router-link>
                </div>

              </div>

            </el-tab-pane>
            <el-tab-pane label="扫码登录" name="second">

              <div class="content">
                <div class="code">
                  <img src="./images/QRCode.png" alt="">
                </div>

                <div class="codetext">
                  请使用手机APP进行扫码
                </div>

                <div class="call clearFix" style="margin-top: 12px">
                  <ul>
                    <li><img src="./images/qq.png" alt=""/></li>
                    <li><img src="./images/sina.png" alt=""/></li>
                    <li><img src="./images/ali.png" alt=""/></li>
                    <li><img src="./images/weixin.png" alt=""/></li>
                  </ul>
                  <router-link class="register" to="/register">立即注册</router-link>
                </div>
              </div>

            </el-tab-pane>
          </el-tabs>

        </div>
      </div>
    </div>

  </div>
  <!--	<el-row type="flex" class="row-bg" justify="center">-->
  <!--		<el-col :xl="6" :lg="7">-->
  <!--			<h2>欢迎来到汽车之家管理系统</h2>-->
  <!--			<el-image :src="require('@/assets/testlogo.png')" style="height: 180px; width: 240px;"></el-image>-->

  <!--			<p>用心打造</p>-->
  <!--			<p>一群汽车爱好者的聚集地</p>-->

  <!--		</el-col>-->

  <!--		<el-col :span="1">-->
  <!--			<el-divider direction="vertical"></el-divider>-->
  <!--		</el-col>-->

  <!--		<el-col :xl="6" :lg="7">-->
  <!--			<el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="80px">-->

  <!--				<el-form-item label="用户名" prop="username" style="width: 380px;">-->
  <!--					<el-input v-model="loginForm.username"></el-input>-->
  <!--				</el-form-item>-->
  <!--				<el-form-item label="密码" prop="password"  style="width: 380px;">-->
  <!--					<el-input v-model="loginForm.password" type="password"></el-input>-->
  <!--				</el-form-item>-->
  <!--				<el-form-item label="验证码" prop="captcha"  style="width: 380px;">-->
  <!--					<el-input v-model="loginForm.captcha"  style="width: 172px; float: left" maxlength="5"></el-input>-->
  <!--					<el-image :src="captImg" class="captImg" @click="getCaptcha"></el-image>-->
  <!--				</el-form-item>-->

  <!--				<el-form-item>-->
  <!--					<el-button type="primary" @click="submitForm('loginForm')">登录</el-button>-->
  <!--					<el-button @click="resetForm('loginForm')">重置</el-button>-->
  <!--				</el-form-item>-->

  <!--			</el-form>-->
  <!--		</el-col>-->
  <!--	</el-row>-->

</template>

<script>

import qs from 'qs'

export default {
  name: "Login",
  data() {
    return {
      activeName: 'first',
      loginForm: {
        username: 'user',
        password: '111111',
        captcha: '',
        token: '',
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
        captcha: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
          {min: 3, max: 5, message: '长度为 3-5 个字符', trigger: 'blur'}
        ],
      },
      captImg: null,
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('/login?' + qs.stringify(this.loginForm)).then(res => {
            console.log(res)
            const jwt = res.headers['authorization']
            localStorage.setItem("token", jwt)
            this.GLOBALVAR.isLogged = true
            this.$router.push("/home")
          });
          this.$message({
            message: '登录成功，欢迎来到汽车之家',
            type: 'success'
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    getCaptcha() {
      this.$axios.get('/captcha').then(res => {

        console.log("/captcha")
        console.log(res)

        this.loginForm.token = res.data.data.token
        this.captImg = res.data.data.captImg
        this.loginForm.captcha = ''
      })
    },
  },
  created() {
    this.getCaptcha()
    this.$root.isInLogPage = true
    console.log("Created! : " + this.$root.isInLogPage)
  },
  mounted() {
    document.getElementsByClassName("el-tabs__header")[0].style.margin = 0;
  },
  destroyed() {
    this.$root.isInLogPage = false
    console.log("Destroy! : " + this.$root.isInLogPage)
  }
}
</script>

<style lang="less" scoped>

.el-row {
  background-color: #fafafa;
  height: 100%;
  display: flex;
  align-items: center;
  text-align: center;
  justify-content: center;
}

.el-divider {
  height: 200px;
}

.captImg {
  float: left;
  margin-left: 8px;
  border-radius: 4px;
}

.login-container {
  .login-wrap {
    height: 487px;
    background-color: #e93854;

    .login {
      width: 1519px;
      height: 487px;
      margin: 0 auto;
      background: url(./images/background.png) no-repeat;
    }

    .loginform {
      width: 420px;
      height: 395px;
      box-sizing: border-box;
      background: #fff;
      float: right;
      top: 45px;
      position: relative;
      padding: 20px;
      margin-right: 130px;

      .tab {

        li {
          width: 50%;
          float: left;
          text-align: center;

          a {
            width: 100%;
            display: block;
            height: 50px;
            line-height: 50px;
            font-size: 20px;
            font-weight: 700;
            color: #333;
            border: 1px solid #ddd;
            box-sizing: border-box;
            text-decoration: none;
          }

          .current {
            border-bottom: none;
            border-top-color: #28a3ef;
            color: #e1251b;
          }
        }
      }

      .content {
        width: 380px;
        height: 316px;
        box-sizing: border-box;
        border: 1px solid #ddd;
        border-top: none;
        padding: 18px;

        form {
          margin: 15px 0 18px 0;
          font-size: 12px;
          line-height: 18px;

          .input-text {
            margin-bottom: 16px;

            span {
              float: left;
              width: 37px;
              height: 32px;
              border: 1px solid #ccc;
              background: url(~@/assets/icons.png) no-repeat -10px -201px;
              box-sizing: border-box;
              border-radius: 2px 0 0 2px;
            }

            .pwd {
              background-position: -72px -201px;
            }

            input {
              width: 302px;
              height: 32px;
              box-sizing: border-box;
              border: 1px solid #ccc;
              border-left: none;
              float: left;
              font-size: 14px;
              line-height: 22px;
              padding: 6px 8px;

              border-radius: 0 2px 2px 0;
              outline: none;
            }
          }

          .setting {
            label {
              float: left;
            }

            .forget {
              float: right;
            }
          }

          .btn {
            background-color: #e1251b;
            padding: 6px;
            border-radius: 0;
            font-size: 16px;
            font-family: 微软雅黑;
            word-spacing: 4px;
            border: 1px solid #e1251b;
            color: #fff;
            width: 100%;
            height: 36px;
            margin-top: 25px;
            outline: none;
          }
        }

        .call {
          margin-top: 30px;

          ul {
            float: left;

            li {
              float: left;
              margin-right: 5px;
            }
          }

          .register {
            float: right;
            font-size: 15px;
            line-height: 38px;
          }

          .register:hover {
            color: #4cb9fc;
            text-decoration: underline;
          }
        }

        .call {
          width: 100%;
        }

        .call ul {
          display: flex;
          flex-direction: row;
          justify-content: center;
        }

        .call ul li {
          list-style: none;
        }
      }
    }
  }

  .copyright {
    width: 1200px;
    margin: 0 auto;
    text-align: center;
    line-height: 24px;

    ul {
      li {
        display: inline-block;
        border-right: 1px solid #e4e4e4;
        padding: 0 20px;
        margin: 15px 0;
      }
    }
  }
}

.code {
  text-align: center;
  margin-top: 25px;
}

.codetext {
  text-align: center;
  margin-top: 10px;
  font-size: 18px;
}

::v-deep .el-tabs__item {
  padding: 0 20px;
  width: 189px;
  height: 40px;
  text-align: center;
  box-sizing: border-box;
  line-height: 40px;
  display: inline-block;
  list-style: none;
  font-size: 14px;
  font-weight: 500;
  color: #303133;
  position: relative;
}
</style>
